package collection;

import java.util.*;

/**
 * list集常用方法:
 * --get():根据下标获取元素
 * --set():将指定元素设置到指定位置，并返回被替换的元素(用时才接收)
 * --重载remove():删除指定位置元素，并返回被删除的元素(用时才接收)
 * --重载add():将指定元素添加到指定位置，理解为插入
 * --subList():获取集合的字节(含头不含尾)
 *
 * 集合排序:
 * --整数排序  Integer
 * --字符串排序 String
 * --字符串对象Point排序  Point
 * --Lambda表达式
 *
 * 如何去重?   set
 */
public class ListDemo01 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Monday");
        list.add("Tuesday");
        list.add("Wednesday");
        list.add("Thursday");
        list.add("Friday");
        list.add("Saturday");
        list.add("Sunday");
        System.out.println("初始集合list:"+list);
        System.out.println("list下标为2的元素为:"+list.get(2));//Wednesday
        System.out.println("list下标为6的元素为:"+list.get(6));//Sunday
        list.set(6,"weekend");
        System.out.println(list);//[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, weekend]
        //重载remove()
        String s1 = list.remove(6);//删除下标为6的元素weekend
        System.out.println(list);//[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
        //重载add()
        list.add(0,"Sunday");//将Sunday插入到下标为0的位置
        System.out.println(list);//[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
        //subList()
        List<String> subList = list.subList(1,6);
        System.out.println(subList);//[Monday, Tuesday, Wednesday, Thursday, Friday]

        /*
         * 集合排序:
         * --整数排序  Integer
         * --字符串排序 String
         * --字符串对象Point排序  Point
         * --Lambda表达式
         */
        Random rand = new Random();
        List<Integer> list1 = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            list1.add( rand.nextInt(100));
        }
        System.out.println("list1"+list1);
        Collections.sort(list1);//自然排序(从小到大)
        System.out.println("list排序后的数据:"+list1);//有序(小到大)

        //字符串排序 String
        List<String> lists = new ArrayList<>();
        lists.add("张三");
        lists.add("李四四");
        lists.add("王五五五");
        Collections.sort(lists, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        });
        System.out.println("字符串升序后为:"+lists);

        Collections.sort(lists,((o1, o2) -> o1.length()-o2.length() ));
        System.out.println("Lambda表达式写法:"+lists);

        //字符串对象Point排序  Point

        List<Point>  listp = new ArrayList<>();
        listp.add(new Point(5,8));
        listp.add(new Point(15,60));
        listp.add(new Point(57,89));
        listp.add(new Point(1,4));
        listp.add(new Point(10,8));
        listp.add(new Point(22,35));
        System.out.println("list原始数据为:"+listp);
        Collections.sort(listp, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
               int len1= o1.getX()*o1.getX()+o1.getY()* o1.getY();
               int len2= o2.getX()*o2.getX()+o2.getY()* o2.getY();
               return len1-len2;
            }
        });//升序返回
        System.out.println(listp);//[(1,4), (5,8), (10,8), (22,35), (15,60), (57,89)]
        listp.sort( ( o1,  o2) ->{
                int len1= o1.getX()*o1.getX()+o1.getY()* o1.getY();
                int len2= o2.getX()*o2.getX()+o2.getY()* o2.getY();
                return len1-len2;
            });
        System.out.println("Lambda表达式排序:"+listp);

        //如何去重?   set
        List<String> listl = new ArrayList<>();
        listl.add("one");
        listl.add("two");
        listl.add("three");
        listl.add("four");
        listl.add("five");
        listl.add("two");
        System.out.println("listl:"+listl);//[one, two, three, four, five, two]

        Set<String> set = new HashSet<>();
        set.addAll(listl);
        System.out.println("set:"+set);//[four, one, two, three, five]
    }
}

